package com.fumihiko.modules.ca.service.impl;


import com.fumihiko.common.exception.BusinessException;
import com.fumihiko.common.utils.DateTimeUtil;
import com.fumihiko.common.utils.Md5Util;
import com.fumihiko.common.utils.SxConstants;
import com.fumihiko.common.utils.TokenProccessor;
import com.fumihiko.modules.ca.dao.UserAppinfoMapper;
import com.fumihiko.modules.ca.dao.UserTokenMapper;
import com.fumihiko.modules.ca.service.UserTokenService;
import com.fumihiko.modules.ca.vo.UserAppinfoVo;
import com.fumihiko.modules.ca.vo.UserTokenVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserTokenServiceImpl implements UserTokenService {

    @Autowired
    private UserTokenMapper userTokenMapper;
    @Autowired
    private UserAppinfoMapper userAppinfoMapper;


    @Override
    @Transactional
    public String getToken(String appId, String appSecret,String timestamp)  throws Exception{

        //UserAppinfoVo userAppinfoVo = userAppinfoMapper.getUserAppinfoVoObj(appId, appSecret);
//        if(null == userAppinfoVo){
//            throw new BusinessException(SxConstants.SERVER_NOT_EXIT_APPID_CODE,"不存在该appId下的应用");
//        }
//        //验证签名
//        String sourSign = appId+appSecret+timestamp+userAppinfoVo.getSignKey();
//        String desSign = Md5Util.md5Hash(sourSign);
//        if(!desSign.equals(sign)){
//            throw new BusinessException(SxConstants.SERVER_SIGN_FAIL_CODE,"验签失败");
//        }

        //UserTokenVo userTokenVo = userTokenMapper.getUserTokenVoObj(appId, appSecret);
        //String token = "";
        //if(null == userTokenVo){
        //   throw new BusinessException(SxConstants.SERVER_NOT_EXIT_APPID_CODE,"不存在该appId下的应用");
        //}

        //单点登录控制
        //userTokenMapper.deleteUserTokenVoObj(userAppinfoVo.getAppId(), userAppinfoVo.getAppSecret());
        UserTokenVo userTokenVoDB = new UserTokenVo();
        userTokenVoDB.setAppId(appId);
        userTokenVoDB.setAppSecret(appSecret);
        String token = TokenProccessor.getInstance().makeToken().replaceAll("\\+","");
        userTokenVoDB.setAccessToken(token);
        userTokenVoDB.setExpirationTime(DateTimeUtil.getChangeDateByHour(DateTimeUtil.getCurrentTime(), SxConstants.HOUR));
        userTokenMapper.insertUserTokenObj(userTokenVoDB);

        return token;
    }

    @Override
    public UserTokenVo getUserTokenVoObjByAccessToken(String accessToken) {
        return userTokenMapper.getUserTokenVoObjByAccessToken(accessToken);
    }

    @Override
    public void deleteUserTokenVoObj(String appId, String appSecret, String accessToken) {
        userTokenMapper.deleteUserTokenVoObj(appId, appSecret, accessToken);
    }

    @Override
    public void insertUserTokenObj(UserTokenVo userTokenVo) {
        userTokenMapper.insertUserTokenObj(userTokenVo);
    }

    @Override
    public List<UserTokenVo> getUserTokenVoObj(String appId, String appSecret) {
        return userTokenMapper.getUserTokenVoObj(appId, appSecret);
    }

    @Override
    public void updateUserTokenVoAreaCode(String areaCode, String accessToken,String sign) {
        UserAppinfoVo userAppinfoVo = userAppinfoMapper.getUserAppinfoList().get(0);
        if(null == userAppinfoVo){
            throw new BusinessException(SxConstants.SERVER_NOT_EXIT_APPID_CODE,"不存在该appId下的应用");
        }
        //验证签名
        String sourSign = areaCode+accessToken+userAppinfoVo.getSignKey();
        String desSign = Md5Util.md5Hash(sourSign);
        if(!desSign.equals(sign)){
            throw new BusinessException(SxConstants.SERVER_SIGN_FAIL_CODE,"验签失败");
        }

        userTokenMapper.updateUserTokenVoAreaCode(areaCode, accessToken);
    }

    @Override
    public List<UserTokenVo> getUserTokenList() {
        return userTokenMapper.getUserTokenList();
    }
}
